package com.video.netty.InChannelHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.video.entity.Record;
import com.video.netty.commons.utils.ChannelUtil;
import com.video.service.webcon.IRecordService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * golang获取会议记录，返回terminal参与过的会议记录，golang发送
 *
 * {"id":"getRecordlist", "terminal":"1"}
 * 返回
 * {"id":"recordlist", "terminal":"1", "list":[{"host":"1", "starttime":"2019-12-17 23:36:38", "endtime":"2019-12-17 23:36:38", "record":"1_2018-12-03_14-54-39.webm", "users":["1", "3", "7"]}] }
 * 失败
 * {"id":"recordlist", "terminal":"1", "list":[ ] }
 */
@BaseChannelHandler.MsgHandler
public class WsGetRecordlistChannelHandler extends BaseChannelHandler {

    @Autowired
    private IRecordService recordService;

    @Autowired
    private ChannelUtil channelUtil;

    @Override
    protected String msgId() {
        return "getRecordlist";
    }

    @Override
    protected void msgHandler(JSONObject json) throws Exception {
        Integer cid = json.getInteger("terminal");
        List<Record> records = recordService.queryByCid(cid);

        Map map = new HashMap();
        map.put("id", "recordlist");
        map.put("terminal", cid);
        map.put("list", records);

        channelUtil.sendMsg(JSON.toJSONString(map));
    }

    @Override
    protected void exceptionHandler(JSONObject json, Exception e) {
        Integer cid = json.getInteger("terminal");
        Map map = new HashMap();
        map.put("id", "recordlist");
        map.put("terminal", cid);
        map.put("list", "[]");

        channelUtil.sendMsg(JSON.toJSONString(map));
    }
}
